package com.baichang.taoshunda.service.impl;


import com.baichang.framework.excelption.ServiceException;
import com.baichang.taoshunda.dao.BussinessDao;
import com.baichang.taoshunda.dao.UserDao;
import com.baichang.taoshunda.entity.Bussiness;
import com.baichang.taoshunda.entity.EasemobModel;
import com.baichang.taoshunda.entity.HxModel;
import com.baichang.taoshunda.entity.User;
import com.baichang.taoshunda.entity.em.UserType;
import com.baichang.taoshunda.service.EasemobService;
import com.baichang.taoshunda.utils.ConvertTypeUtils;
import com.baichang.taoshunda.utils.EasemobUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;

/**
 * 环信Service实现
 * Created by Dylan on 2015/12/9.
 */
@Service
public class EasemobServiceImpl implements EasemobService {

    @Autowired
    UserDao userDao;

    @Autowired
    BussinessDao companyDao;


    /**
     * 环信调用登录
     *
     * @param userName
     * @param passWord
     * @param userType
     * @return
     */
    @Override
    public Map<String, Object> login(String userName, String passWord, Integer userType) throws ServiceException {
        Map<String, Object> map = new HashMap<String, Object>();
        Integer id = 0;
        String returnUserType = "";
        //修理厂
        if (userType == 0) {
            User depot = userDao.getUserByUserName(userName);
            if (depot == null)
                throw new ServiceException("修理厂用户不存在");
            if (!depot.getPwd().equals(passWord))
                throw new ServiceException("密码不正确");
            id = depot.getId();
            returnUserType = UserType.用户.getCode();
            //商家
        } else if (userType == 1) {
            Bussiness company = companyDao.getBussnessByPhone(userName);
            if (company == null)
                throw new ServiceException("商家用户不存在");
            if (!company.getPwd().equals(passWord))
                throw new ServiceException("密码不正确");
            id = company.getId();
            returnUserType = UserType.商家.getCode();
        }
        map.put("userId", id);
        map.put("userType", returnUserType);
        return map;
    }

    @Override
    public HxModel login2(String userName, String passWord, Integer userType) throws ServiceException {
        HxModel hxModel = new HxModel();
        Integer id = 0;
        String returnUserType = "";
        //修理厂
        if (userType == 0) {
            User depot = userDao.getUserByUserName(userName);
            if (depot == null)
                throw new ServiceException("修理厂用户不存在");
            if (!depot.getPwd().equals(passWord))
                throw new ServiceException("密码不正确");
            id = depot.getId();
            returnUserType = UserType.用户.getCode();
            //商家
        } else if (userType == 1) {
            Bussiness company = companyDao.getBussnessByPhone(userName);
            if (company == null)
                throw new ServiceException("商家用户不存在");
            if (!company.getPwd().equals(passWord))
                throw new ServiceException("密码不正确");
            id = company.getId();
            returnUserType = UserType.商家.getCode();
            //客服
        }
        hxModel.setUserId(id.toString());
        hxModel.setUserType(returnUserType);
        return hxModel;
    }


    /**
     * 注册环信
     *
     * @param id
     * @param userType
     * @param photo
     * @param name
     * @return
     */
    @Override
    public boolean register(Object id, UserType userType, String photo, String name) {
        boolean bl;
        try {
            bl = EasemobUtils.register(id.toString(), userType, photo, name);
        } catch (Exception e) {
            bl = false;
        }
        return bl;
    }

    /**
     * 获取用户信息
     *
     * @param userId
     * @param userType
     * @return
     */
    @Override
    public EasemobModel getUser(Object userId, UserType userType) throws ParseException {
        EasemobModel easemobModel = new EasemobModel();
        Map<String, Object> map = null;
        try {
            map = EasemobUtils.getUser(userId.toString(), userType);
        } catch (Exception e) {
            return easemobModel;
        } finally {
            if (map != null && map.size() > 0) {
                easemobModel.setKid(map.get("kid").toString());
                easemobModel.setUserType(UserType.valueOfCode(map.get("userType").toString()));
                easemobModel.setCreateTime(ConvertTypeUtils.obj2DateTime(map.get("createTime")));
                easemobModel.setUserId(map.get("userId").toString());
                easemobModel.setEmId(map.get("emId").toString());
                easemobModel.setEmPwd(map.get("emPwd").toString());
                easemobModel.setName(map.get("name").toString());
                easemobModel.setPhoto(map.get("photo").toString());
            }
        }
        return easemobModel;
    }


    /**
     * 修改用户信息
     *
     * @param easemobModel
     * @return
     */
    @Override
    public boolean update(EasemobModel easemobModel) {
        boolean bl = false;
        if (easemobModel != null) {
            try {
                bl = EasemobUtils.update(easemobModel.getKid(), easemobModel.getUserType(), easemobModel.getName(), easemobModel.getPhoto());
            } catch (Exception e) {
                bl = false;
            }
        }
        return bl;
    }

    /**
     * 删除环信信息
     *
     * @param kid
     * @return
     */
    @Override
    public boolean delete(String kid) {
        boolean bl;
        try {
            bl = EasemobUtils.delete(kid);
        } catch (Exception e) {
            bl = false;
        }
        return bl;
    }

    public static void main(String[] args) throws ParseException {
        EasemobService easemobService = new EasemobServiceImpl();
        System.out.println(easemobService.getUser(2, UserType.用户).getEmId());
    }
}
